# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=ogitor
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=r803.b2b65b38982f
pkgrel=1
pkgdesc="WYSIWYG scene editing environment for OGRE (mingw-w64)"
arch=('any')
url="http://www.ogre3d.org/"
license=("LGPL")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-pkg-config" "${MINGW_PACKAGE_PREFIX}-cmake" "mercurial")
depends=("${MINGW_PACKAGE_PREFIX}-libwinpthread"
        "${MINGW_PACKAGE_PREFIX}-ogre3d"
        "${MINGW_PACKAGE_PREFIX}-boost"
        "${MINGW_PACKAGE_PREFIX}-qt5")
options=('staticlibs' '!strip' '!buildflags')
install=ogitor.install
source=("${_realname}-${pkgver}.tar.xz"
        https://bitbucket.org/jacmoe/ogitor/downloads/media.zip
        https://bitbucket.org/jacmoe/ogitor/downloads/projects.zip
        cmake-commands-fix.patch
        dont-install-3rdparty-libraries.patch
        disable-msvc-flags.patch)
md5sums=('b5d6be448342ce20b332db7da2d53984'
         'c43578ed01e383a0eebd0719ae4241f8'
         'b93a9391b3d6e4d835e6a9bfffb3e6e9'
         '374ef421e21cef78a40b9c9e36d8e49e'
         '9899396338dcd1d2464e1be760308415'
         'd44bff3e3441b8c0a18bd5bab4da598a')

# source PKGBUILD && mksource
mksource() {
  [[ -x /usr/bin/hg ]] || (echo "hg not found. Install mercurial." && return 1)

  __hgroot='https://bitbucket.org/jacmoe'
  __hgrepo="${_realname}"
  __hgbranch='default'
  __hgrev="qt5"

  hg clone -u ${__hgrev} "${__hgroot}/${__hgrepo}/" ${__hgrepo}
  #hg clone -b ${__hgbranch} "${__hgroot}${__hgrepo}#${__hgrev}" ${__hgrepo}

  pushd ${__hgrepo}
  if (( $(hg id -n) < $(hg id -nr ${__hgbranch}) )); then
    printf 'You are not building the latest revision!\n'
    printf "Consider updating __hgrev to $(hg id -r ${__hgbranch}).\n"
  fi
  __hgver="r$(hg identify -n).$(hg identify -i)"
  popd

  mv ${__hgrepo} ${_realname}-${__hgver}
  find ${_realname}-${__hgver} -depth -type d -name .hg -exec rm -rf {} \;
  rm ${_realname}-${__hgver}/{.hgignore,.hgtags}
  tar -cJf ${_realname}-${__hgver}.tar.xz ${_realname}-${__hgver}/*
  rm -r ${_realname}-${__hgver}

  gpg --detach-sign ${_realname}-${__hgver}.tar.xz

  #scp ${pkgname}-${__hgver}.tar.xz nym:/srv/ftp/other/vim/
  #scp ${pkgname}-${__hgver}.tar.xz.sig nym:/srv/ftp/other/vim/
}

#pkgver() {
#  cd "$srcdir/repo"
#  printf "r%s.%s" "$(hg identify -n)" "$(hg identify -i)"
#}

prepare() {
  mv ${srcdir}/Media ${srcdir}/${_realname}-${pkgver}/RunPath/
  mv ${srcdir}/Projects ${srcdir}/${_realname}-${pkgver}/RunPath/
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i ${srcdir}/cmake-commands-fix.patch
  patch -p1 -i ${srcdir}/dont-install-3rdparty-libraries.patch
  patch -p1 -i ${srcdir}/disable-msvc-flags.patch
}

build() {
  mkdir -p ${pkgdir}${MINGW_PREFIX}
  pushd ${pkgdir}${MINGW_PREFIX} > /dev/null
  export PREFIX_WIN=`pwd -W`
  popd > /dev/null

  pushd ${MINGW_PREFIX} > /dev/null
  export PREFIX_DEPS=`pwd -W`
  popd > /dev/null

  export OGRE_HOME=${PREFIX_DEPS}
  export QTDIR=${PREFIX_DEPS}

    cd "${srcdir}/${_realname}-${pkgver}"
    ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX:PATH=${PREFIX_WIN} \
    -DCMAKE_BUILD_TYPE:STRING=RelWithDebInfo \
    -DCMAKE_LIBRARY_PATH="${PREFIX_DEPS}/lib;${PREFIX_DEPS}/${MINGW_CHOST}/lib"
    make
}

package() {
    cd "${srcdir}/${_realname}-${pkgver}"
    make -j1 install #DESTDIR="$pkgdir"
  pushd ${pkgdir}${MINGW_PREFIX} > /dev/null
      rm -rf CMakeFiles
    rm cmake_install.cmake
    rm Makefile
    mv bin/plugins.cfg bin/plugins_ogitor.cfg
    mv bin/plugins_debug.cfg bin/plugins_debug_ogitor.cfg
    mv bin/resources.cfg bin/resources_ogitor.cfg
  popd > /dev/null
}
